package homework.ch11_13.p3;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Course implements Cloneable {
    private String courseName;
    private List<Person> students=new ArrayList<Person>();
    private Person teacher;

    public Course(String courseName, Person teacher) {
        this.courseName = courseName;
        this.teacher = teacher;
    }

    public Object clone() {
        Course course = new Course(this.courseName, this.teacher);
        course.students = new ArrayList<>(this.students);
        return course;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Course)) return false;
        Course course = (Course) o;
        return courseName.equals(course.courseName) && this.getNumberofStudent()==((Course) o).getNumberofStudent()&&this.students.contains(o) && teacher.equals(course.teacher);
    }

    @Override
    public int hashCode() {
        return Objects.hash(courseName, students, teacher);
    }

    public String toString(){
        StringBuffer str=new StringBuffer();
        for(Person s:this.students){
            str.append("").append(s.toString());
        }
        return "coueseName:"+this.courseName+'\n'
                +"Teacher Info: " +this.teacher+'\n'
                +"Student List:"+'\n'+str
                +"Totally:" +this.getNumberofStudent()+" sdudents.";
    }

    public String getCourseName() {
        return courseName;
    }

    public List<Person> getStudents() {
        return students;
    }

    public Person getTeacher() {
        return teacher;
    }

    public int getNumberofStudent(){
        return this.students.size();
    }

    public void unregister(Person s){
        for(int i=0;i<getNumberofStudent();i++){
            if(s.equals(this.students.get(i))){
                this.students.remove(s);
            }
        }
    }

    public void register(Person s){
        int flag=1;
        for(int i=0;i<this.getNumberofStudent();i++){
            if(this.students.get(i).equals(s)==true)
                flag=0;
        }
        if(flag==1)
            this.students.add(s);
    }
}